package com.example.demo.controller;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.example.demo.controller.bean.Result;
import com.example.demo.mapper.entity.StudentTask;
import com.example.demo.mapper.entity.Task;
import com.example.demo.service.StudentCouponsService;
import com.example.demo.service.StudentPointsService;
import com.example.demo.service.StudentTaskService;
import com.example.demo.utils.RedisUtil;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Lucy
 * @create 2024-03-06 11:05
 */
@RequestMapping("camera")
@Api(value = "摄像头识别", tags = {"摄像头识别接口"})
@RestController
@Slf4j
public class CameraController {

    @Autowired
    private StudentTaskService taskService;

    @Autowired
    private StudentPointsService pointsService;

    @Autowired
    private StudentCouponsService couponsService;

    @Autowired
    private RedisUtil redisUtil;
    @PostMapping("/processQRCodeData")
    public Result processQRCodeData(@RequestBody String data) {
        System.out.println(data);
        JSONObject jsonData = JSONUtil.parseObj(data);
        if (!jsonData.containsKey("QR_content")) return Result.warn("无效二维码");
        String qrCodeContent = jsonData.getStr("QR_content");
        if (!qrCodeContent.startsWith("stkaaa")) return Result.warn("无效二维码");
        String[] result = qrCodeContent.split("(?<=\\G.{6})");
        String formattedStudentId = result[2];
        String formattedTaskId = result[3];
        int studentId = Integer.parseInt(formattedStudentId);
        int taskId = Integer.parseInt(formattedTaskId);
        String key = "QRCODE" + ":" + formattedStudentId + ":" + formattedTaskId;
        if (!redisUtil.hasKey(key)) return Result.warn("二维码已过期");
        redisUtil.del(key);
        taskService.updateCompletionStatus(studentId, taskId, 2);
        Task task = taskService.selectTaskById(taskId);
        //获取完成任务的积分奖励
        pointsService.updateStudentPoints(studentId, task.getPointsAwarded(), 0);
        //获取完成任务的属性奖励和徽章奖励
        taskService.receiveTaskReward(studentId, taskId);
        //获取完成任务的优惠券奖励
        if(task.getCouponsAwarded() != 0) couponsService.insertStudentCoupons(studentId, task.getCouponsAwarded());
        return Result.ok("成功识别");
    }

    @PostMapping("/processFaceRecogData")
    public Result processFaceRecogData(@RequestBody String data) {
        System.out.println(data);
        JSONObject jsonData = JSONUtil.parseObj(data);
        int taskId = Integer.parseInt(jsonData.getStr("taskId"));
        int studentId = Integer.parseInt(jsonData.getStr("studentId"));
        StudentTask studentTask = taskService.selectStudentTask(studentId, taskId);
        if (studentTask.getCompletionStatus() == 2) return Result.warn("已识别");
        taskService.updateCompletionStatus(studentId, taskId, 2);
        Task task = taskService.selectTaskById(taskId);
        //获取完成任务的积分奖励
        pointsService.updateStudentPoints(studentId, task.getPointsAwarded(), 0);
        //获取完成任务的属性奖励和徽章奖励
        taskService.receiveTaskReward(studentId, taskId);
        //获取完成任务的优惠券奖励
        if(task.getCouponsAwarded() != 0) couponsService.insertStudentCoupons(studentId, task.getCouponsAwarded());
        return Result.ok("成功识别");
    }
}